/**
 *	@file	an_puzzle_sub.c
 *	@brief	アンノーンパズル内　サブルーチン
 *	@author	Miyuki Iwasawa
 *	@date	08.05.28
 */
#include "common.h"
#include "system/procsys.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/snd_tool.h"
#include "system/fontproc.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/buflen.h"
#include "system/window.h"
#include "system/brightness.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/palanm.h"
#include "system/wipe.h"
#include "system/window.h"
#include "system/font_arc.h"
#include "system/winframe.naix"
#include "system/touch_subwindow.h"
#include "gflib/touchpanel.h"
#include "gflib/strbuf_family.h"
#include "field/field_place_name.h"

#include "application/app_tool.h"
#include "application/an_puzzle.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_an_puzzle.h"
#include "an_puzzle_gra.naix"
#include "an_puzzle_sys.h"
#include "an_puzzle_sub.h"
#include "system/winframe.naix"

#include "an_puzzle.dat"
#include "an_puzzle_snd_def.h"
#include "data/clact_util/clact_header.naix"

////////////////////////////////////////////////////////////////
///データ定義
static const TCATS_OAM_INIT an_puzzle_coi = {
	0,128,		///< メイン	OAM管理領域・開始/終了
	0,32,		///< メイン	アフィン管理領域・開始/終了
	0,128,		///< サブ	OAM管理領域・開始/終了
	0,32,		///< サブ	アフィン管理領域・開始/終了
};
static const TCATS_CHAR_MANAGER_MAKE an_puzzle_ccmm = {
	AN_PUZZLE_ACTRES_MAX,	//ID制御数
	0*1024,	//メイン画面管理サイズ(byte単位) 0/64kbyte
	0*1024,	//サブ画面管理サイズ(byte単位) 0/16kbyte
	GX_OBJVRAMMODE_CHAR_1D_32K,	//メインOBJモード指定
	GX_OBJVRAMMODE_CHAR_1D_32K,	//サブOBJモード指定
};

///TCATS_RES_ARC
static const TCATS_RESOURCE_FILE_LIST an_puzzle_actres_list = {
	NARC_clact_header_an_puzzle_chr_resdat,	// キャラクタ
	NARC_clact_header_an_puzzle_pal_resdat,		// パレット
	NARC_clact_header_an_puzzle_cell_resdat,	// セル
	NARC_clact_header_an_puzzle_canm_resdat,	// セルアニメ
	TCATS_RESID_NULL,							// マルチセル
	TCATS_RESID_NULL,							// マルチセルアニメ
	NARC_clact_header_an_puzzle_celact_cldat		// ヘッダー
};

///タッチパネル制御テーブル
static const RECT_HIT_TBL TpPicSelectRect[] = {
//	{0,191,0,255}, ty,by,lx,rx
	{TP_BACK_PY,TP_BACK_PY+TP_BACK_H,TP_BACK_PX,TP_BACK_PX+TP_BACK_W},
	{RECT_HIT_END,0,0,0}
};

#define GridPosX(x)	(x*ACT_PANEL_OX+ACT_PANEL_PX)
#define GridPosY(y)	(y*ACT_PANEL_OY+ACT_PANEL_PY)

////////////////////////////////////////////////////////////////
///プロトタイプ
////////////////////////////////////////////////////////////////
static int touch_HitCheckPanel(AN_PUZZLE_WK* wk,u16 x, u16 y);

static void AnPuzzleVBlank(void * work);

static void sub_PanelPosInit(AN_PUZZLE_WK* wk);
static void sub_VBankSet(void);
static void sub_BGLInit(AN_PUZZLE_WK* wk);
static void sub_BGLRelease(AN_PUZZLE_WK* wk);
static void sub_2DResInit(AN_PUZZLE_WK* wk);
static void sub_2DResRelease(AN_PUZZLE_WK* wk);

static void sub_MsgDataInit(AN_PUZZLE_WK* wk);
static void sub_MsgDataRelease(AN_PUZZLE_WK* wk);
static void sub_BmpWinAdd(AN_PUZZLE_WK* wk);
static void sub_BmpWinDel(AN_PUZZLE_WK* wk);
static void sub_ActorSysInit(AN_PUZZLE_WK* wk);
static void sub_ActorSysRelease(AN_PUZZLE_WK* wk);
static void sub_ActorInit(AN_PUZZLE_WK* wk);
static void sub_ActorRelease(AN_PUZZLE_WK* wk);
static void sub_ActorObjMake(AN_PUZZLE_WK* wk);
static void sub_ActorObjDelete(AN_PUZZLE_WK* wk);

static BOOL sub_IsPerfect(AN_PUZZLE_WK* wk);
static void sub_BackButtonWrite(AN_PUZZLE_WK* wk,u8 pat);
static void sub_WinPokeWrite(AN_PUZZLE_WK* wk);
static void sub_WinMsgWrite(AN_PUZZLE_WK* wk,u8 midx,BOOL winf_on,u8 spd);
static void sub_PanelGuideDrawCore(AN_PUZZLE_WK* wk,u8 gx,u8 gy,u8 pat);
static void sub_PanelGuideDraw(AN_PUZZLE_WK* wk,u8 gx,u8 gy);

static int sub_InputChangeSelect(AN_PUZZLE_WK* wk);
static void sub_YesNoBtnSet(AN_PUZZLE_WK* wk);
static int sub_YesNoBtnWait(AN_PUZZLE_WK* wk);

static void sub_PanelActPosSet(PANEL_WK* pp, s16 x, s16 y);
static void sub_PanelGridPosSet(AN_PUZZLE_WK* wk,u8 idx,u8 x,u8 y,u8 rot);
static void sub_TargetPanelStateSet(AN_PUZZLE_WK* wk,u8 idx,BOOL set);


static void sub_BackBtnAnimeSet(AN_PUZZLE_WK* wk);
static void tcb_BackBtnAnime( TCB_PTR tcb, void* p_work );

////////////////////////////////////////////////////////////////
///グローバル関数
////////////////////////////////////////////////////////////////
/**
 *	@brief	リソース初期化
 */
void AnPuzzleResInit(AN_PUZZLE_WK* wk)
{
	sub_PanelPosInit( wk );
	sub_BGLInit( wk );
	sub_2DResInit( wk );
	sub_MsgDataInit( wk );
	sub_BmpWinAdd( wk );
	sub_ActorInit( wk );
	sys_VBlankFuncChange(AnPuzzleVBlank, wk);
}

/**
 *	@brief	リソース解放
 */
void AnPuzzleResRelease(AN_PUZZLE_WK* wk)
{
	sub_ActorRelease( wk );
	sub_BmpWinDel( wk );
	sub_MsgDataRelease( wk );
	sub_2DResRelease( wk );
	sub_BGLRelease( wk );
}

/**
 *	@brief	画面初期書き込み
 */
void AnPuzzleSub_InitialDraw(AN_PUZZLE_WK* wk)
{
	sub_BackButtonWrite(wk,ANM_RET);
	sub_WinPokeWrite(wk);

	GF_BGL_LoadScreenV_Req( wk->bgl, DFRM_EFF );
	GF_BGL_LoadScreenV_Req( wk->bgl, UFRM_MSG );
}

/**
 *	@brief	入力取得　パネル選択
 */
int AnPuzzleSub_InputSelect(AN_PUZZLE_WK* wk)
{
	int	ret;

	if(!GF_TP_GetTrg()){
		return SEQ_PUZZLE_MAIN;
	}
	ret = GF_TP_RectHitTrg(TpPicSelectRect);
	if(ret == 0){
		wk->key_mode = APP_KTST_TOUCH;
		sub_BackBtnAnimeSet(wk);
		Snd_SePlay(SND_PUZZLE_BACK);
		return SEQ_END_CHECK;
	}
	//パネルタッチチェック
	ret = touch_HitCheckPanel(wk,sys.tp_x,sys.tp_y);
	if(ret < 0){
		return SEQ_PUZZLE_MAIN;
	}
	//ターゲット設定
	sub_TargetPanelStateSet(wk,ret,TRUE);
	Snd_SePlay(SND_PUZZLE_TOUCH_PEACE);
	wk->key_mode = APP_KTST_TOUCH;
	return SEQ_MOVE_CHECK;
}

/**
 *	@brief	入力取得　パネル動作チェック
 */
int AnPuzzleSub_InputMoveCheck(AN_PUZZLE_WK *wk)
{
	if(!GF_TP_GetCont()){
		wk->sub_ct = 0;
		return SEQ_ROT_EFF;
	}

	if(wk->sub_ct++ >= 2){
		wk->sub_ct = 0;
		wk->cpos_x = GridPosX(wk->target->x)+(ACT_PANEL_W/2);
		wk->cpos_y = GridPosY(wk->target->y)+(ACT_PANEL_H/2);
		wk->gpos_x = wk->target->x;
		wk->gpos_y = wk->target->y;
		//ガイドセット
		sub_PanelGuideDrawCore(wk,wk->target->x,wk->target->y,G_CONF);
		return SEQ_MOVE;
	}
	return SEQ_MOVE_CHECK;
}

/**
 *	@brief	パネルあたり判定チェック
 */
static int touch_HitCheckPanel(AN_PUZZLE_WK* wk,u16 x, u16 y)
{
	int i;
	u16 px,py;

	if( x < TP_PANEL_SX || x >= TP_PANEL_EX){
		return -1;
	}
	//タッチグリッド座標をチェック
	px = ((x-TP_PANEL_SX) / TP_PANEL_W);
	py = (y / TP_PANEL_H);
	for(i = 0;i < PANEL_MAX;i++){
		if(wk->panel[i].x == px && wk->panel[i].y == py){
			if(wk->panel[i].rock){
				return -1;
			}
			return i;
		}
	}
	return -1;
}

/**
 *	@brief	移動候補のグリッドを検出
 */
static int touch_CheckMovePos(AN_PUZZLE_WK* wk,u8* gx,u8* gy)
{
	int i;
	u16 px,py;
	static const u8 ignore[][2] = {
		{0,0},{(PANEL_FLDX-1),0},{0,(PANEL_FLDY-1)},{(PANEL_FLDX-1),(PANEL_FLDY-1)}};

	*gx = 0;
	*gy = 0;

	if( wk->cpos_x < TP_PANEL_SX || wk->cpos_x >= TP_PANEL_EX){
		return FALSE;
	}
	//タッチグリッド座標をチェック
	px = ((wk->cpos_x-TP_PANEL_SX) / TP_PANEL_W);
	py = (wk->cpos_y / TP_PANEL_H);

	//無効座標チェック
	for(i = 0;i < 4;i++){
		if(px == ignore[i][0] && py == ignore[i][1]){
			return FALSE;
		}
	}
	//現在位置が空きグリッドかどうかチェック
	for(i = 0;i < PANEL_MAX;i++){
		if(wk->tidx == i){
			continue;	//自分の位置はチェックしなくていい
		}
		if(wk->panel[i].x == px && wk->panel[i].y == py){
			return FALSE;
		}
	}

	*gx = px;
	*gy = py;
	return TRUE;
}

/**
 *	@brief	入力取得　パネルドラッグ移動
 */
int AnPuzzleSub_InputMove(AN_PUZZLE_WK* wk)
{
	int ret;
	BOOL exe_f = FALSE;
	s16 px,py;

	if(!GF_TP_GetCont()){	//放された
		//ガイド表示Off
		sub_PanelGuideDrawCore(wk,wk->target->x,wk->target->y,G_NULL);
		sub_PanelGuideDrawCore(wk,wk->gpos_x,wk->gpos_y,G_NULL);
		GF_BGL_LoadScreenV_Req( wk->bgl, DFRM_EFF );

		//新座標設定
		if(wk->gpos_x == 0 && wk->gpos_y == 0){
			Snd_SePlay(SND_PUZZLE_RELEASE_PEACE);
			sub_PanelGridPosSet(wk,wk->tidx,wk->target->x,wk->target->y,wk->target->rot);
		}else{
			Snd_SePlay(SND_PUZZLE_SET_PEACE);
			sub_PanelGridPosSet(wk,wk->tidx,wk->gpos_x,wk->gpos_y,wk->target->rot);
		}
		sub_TargetPanelStateSet(wk,TARGET_NULL,FALSE);

		//完成チェック
		if(sub_IsPerfect(wk)){
			return SEQ_PERFECT;
		}else{
			return SEQ_PUZZLE_MAIN;
		}
	}
	px = sys.tp_x;
	py = sys.tp_y;

	wk->opos_x = wk->cpos_x;
	wk->opos_y = wk->cpos_y;

	//移動限界補正
	if(py < ACT_PANEL_LU){
		py = ACT_PANEL_LU;
	}else if(py > ACT_PANEL_LD){
		py = ACT_PANEL_LD;
	}
	if(px < ACT_PANEL_LL){
		px = ACT_PANEL_LL;
	}else if(px > ACT_PANEL_LR){
		px = ACT_PANEL_LR;
	}
	//ドラッグ
	CATS_ObjectPosSet(wk->pAct[ACT_SHADOW],px,py);
	sub_PanelActPosSet(wk->target, px+ACT_PANEL_OFS,py+ACT_PANEL_OFS);

	//現在の座標を記憶
	wk->cpos_x = px;
	wk->cpos_y = py;

	//ガイド表示の更新
	{
		u8 gx,gy;

		touch_CheckMovePos(wk,&gx,&gy);
		if(wk->gpos_x != gx || wk->gpos_y != gy){
			sub_PanelGuideDraw(wk,gx,gy);
			wk->gpos_x = gx;
			wk->gpos_y = gy;
		}

	}
	return SEQ_MOVE;
}

/**
 *	@brief	パネル回転エフェクト
 */
int AnPuzzleSub_PanelRotEffect(AN_PUZZLE_WK* wk)
{
	u16 rot;

	switch(wk->sub_seq){
	case 0:
		Snd_SePlay(SND_PUZZLE_ROT_PEACE);
		wk->sub_seq++;
		break;
	case 1:
		//回転エフェクト
		rot = 2048*wk->sub_ct++;
		CLACT_SetRotation(wk->target->act, rot+(wk->target->rot*ROT90) );
		if(wk->sub_ct >= 8){
			wk->sub_seq++;
		}
		break;
	case 2:
		//ロット値更新
		wk->target->rot = (wk->target->rot+1)%4;
		//ターゲット解放設定
		sub_TargetPanelStateSet(wk,TARGET_NULL,FALSE);

		wk->sub_ct = 0;
		wk->sub_seq = 0;

		//完成チェック
		if(sub_IsPerfect(wk)){
			return SEQ_PERFECT;
		}else{
			return SEQ_PUZZLE_MAIN;
		}
	}
	return SEQ_ROT_EFF;
}


/**
 *	@brief	終了確認シーケンス
 */
int AnPuzzleSub_EndCheck(AN_PUZZLE_WK* wk)
{
	int ret;

	switch(wk->sub_seq){
	case 0:
		if(wk->btnanm_f){
			break;
		}
		sub_WinMsgWrite(wk,MIDX_EXP_ENDCHK,TRUE,wk->msg_spd);
		wk->sub_seq++;
		break;
	case 1:
		if( GF_MSG_PrintEndCheck( wk->msg_idx )){
			break;
		}
		sub_YesNoBtnSet(wk);
		wk->sub_seq++;
		break;
	case 2:
		ret = sub_YesNoBtnWait(wk);
		if(ret == SEQ_END_CHECK){
			break;
		}
		wk->sub_seq = 0;
		return ret;
	}
	return SEQ_END_CHECK;
}

/**
 *	@brief	完成エフェクト
 */
int AnPuzzleSub_PerfectEffect(AN_PUZZLE_WK* wk)
{
	switch(wk->sub_seq){
	case 0:
		Snd_SePlay(SND_PUZZLE_COMPLETE);
		wk->sub_seq++;
		break;
	case 1:
		SoftFadePfd(wk->palAnm,FADE_MAIN_OBJ,16*2+11,5,wk->sub_ct,0x7FFF);
		if(wk->sub_ct++ >= 15){
			wk->sub_seq++;
		}
		break;
	case 2:
		SoftFadePfd(wk->palAnm,FADE_MAIN_OBJ,16*2+11,5,wk->sub_ct,0x7FFF);
		if(wk->sub_ct-- == 0){
			wk->sub_seq++;
		}
		break;
	default:
		wk->sub_seq = 0;
		wk->sub_ct = 0;
		//完成フラグセット
		wk->is_clear = TRUE;
		return SEQ_FADE_OUT;
	}
	return SEQ_PERFECT;
}

////////////////////////////////////////////////////////////////
///ローカル関数
////////////////////////////////////////////////////////////////
/**
 *	@brief	VBlank関数
 */
static void AnPuzzleVBlank(void * work)
{
	AN_PUZZLE_WK* wk = (AN_PUZZLE_WK*)work;

	//パレット転送
	if( wk->palAnm != NULL ){
		PaletteFadeTrans( wk->palAnm );
	}

	//アクター転送
	if(wk->pActSys != NULL){
		CATS_RenderOamTrans();
	}
	NNS_GfdDoVramTransfer();	//VRam転送マネージャ実行
	GF_BGL_VBlankFunc( wk->bgl );
	OS_SetIrqCheckFlag( OS_IE_V_BLANK);
}

/**
 *	@brief	初期配置データセット
 */
static void sub_PanelPosInit(AN_PUZZLE_WK* wk)
{
	int i,j;
	const PANEL *src,*pp;
	PANEL_WK *pwk;

	static const PANEL *tbl[] = {
		AnPuzzleDataLv01,AnPuzzleDataLv02,AnPuzzleDataLv03,AnPuzzleDataLv04};

	src = tbl[wk->level];

	for(i = 0;i < PANEL_FLDY;i++){
		for(j = 0;j < PANEL_FLDX;j++){
			pp = &(src[i*PANEL_FLDX+j]);
			if(pp->no == 0 || pp->no > PANEL_MAX){
				continue;
			}
			pwk = &wk->panel[pp->no-1];

			pwk->x = j;
			pwk->y = i;
			pwk->rot = pp->rot;
			pwk->rock = pp->rock;
			pwk->act = NULL;	//アクターワークは後で設定
		}
	}
}

/**
 *	@brief	vramバンク設定
 */
static void sub_VBankSet(void)
{
	GF_BGL_DISPVRAM vramSetTable = {
		GX_VRAM_BG_128_A,				// メイン2DエンジンのBG
		GX_VRAM_BGEXTPLTT_NONE,			// メイン2DエンジンのBG拡張パレット
		GX_VRAM_SUB_BG_128_C,			// サブ2DエンジンのBG
		GX_VRAM_SUB_BGEXTPLTT_NONE,		// サブ2DエンジンのBG拡張パレット
		GX_VRAM_OBJ_64_E,				// メイン2DエンジンのOBJ
		GX_VRAM_OBJEXTPLTT_NONE,		// メイン2DエンジンのOBJ拡張パレット
		GX_VRAM_SUB_OBJ_16_I,			// サブ2DエンジンのOBJ
		GX_VRAM_SUB_OBJEXTPLTT_NONE,	// サブ2DエンジンのOBJ拡張パレット
		GX_VRAM_TEX_NONE,				// テクスチャイメージスロット
		GX_VRAM_TEXPLTT_NONE			// テクスチャパレットスロット
	};
	GF_Disp_SetBank( &vramSetTable );
}


/*
 *	@brief	BGL初期化
 */
static void sub_BGLInit(AN_PUZZLE_WK* wk)
{
	sub_VBankSet();

	wk->bgl = GF_BGL_BglIniAlloc(wk->heapID);
	{	//BG SYSTEM
		GF_BGL_SYS_HEADER BGsys_data = {
			GX_DISPMODE_GRAPHICS,GX_BGMODE_0,GX_BGMODE_0,GX_BG0_AS_2D
		};
		GF_BGL_InitBG(&BGsys_data);
	}
	//上画面をサブ,下画面をメインに切り替え
	GX_SetDispSelect(GX_DISP_SELECT_SUB_MAIN);

	{	// 上画面　テキスト面
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf800, GX_BG_CHARBASE_0x10000, GX_BG_EXTPLTT_01,
			UFRM_MSG_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, UFRM_MSG, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, UFRM_MSG );
	}

	{	// 上画面　背景
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01,
			UFRM_STONE_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, UFRM_STONE, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, UFRM_STONE );
	}

	{	// 上画面　背景
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe800, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01,
			UFRM_BACK_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, UFRM_BACK, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, UFRM_BACK );
	}

	{	// 下画面　テキスト面
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf800, GX_BG_CHARBASE_0x10000, GX_BG_EXTPLTT_01,
			DFRM_MSG_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, DFRM_MSG, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, DFRM_MSG );
	}

	{	// 下画面　テキスト面
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01,
			DFRM_EFF_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, DFRM_EFF, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, DFRM_EFF );
	}

	{	// 下画面　背景
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe800, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01,
			DFRM_BACK_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, DFRM_BACK, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, DFRM_BACK );
	}


	GF_BGL_ClearCharSet( UFRM_MSG, 32, 0, wk->heapID );
	GF_BGL_ClearCharSet( UFRM_BACK, 32, 0, wk->heapID );
	GF_BGL_ClearCharSet( DFRM_MSG, 32, 0, wk->heapID );
	GF_BGL_ClearCharSet( DFRM_BACK, 64, 0, wk->heapID );
}

/**
 * @brief	BGL解放
 */
static void sub_BGLRelease(AN_PUZZLE_WK* wk)
{

	GF_BGL_BGControlExit(wk->bgl, DFRM_BACK );
	GF_BGL_BGControlExit(wk->bgl, DFRM_EFF );
	GF_BGL_BGControlExit(wk->bgl, DFRM_MSG );
	GF_BGL_BGControlExit(wk->bgl, UFRM_BACK );
	GF_BGL_BGControlExit(wk->bgl, UFRM_STONE );
	GF_BGL_BGControlExit(wk->bgl, UFRM_MSG );

	sys_FreeMemoryEz(wk->bgl);

	//上画面をメイン,下画面をサブに戻す
	GX_SetDispSelect(GX_DISP_SELECT_MAIN_SUB);
}

/**
 * @brief	2Dリソース初期化
 */
static void sub_2DResInit(AN_PUZZLE_WK* wk)
{
	ARCHANDLE* handle;

	//アーカイブのハンドルを取得
	handle = ArchiveDataHandleOpen(ARC_AN_PUZZLE_GRA,wk->heapID);

	//パレットアニメコントローラー確保
	wk->palAnm = PaletteFadeInit(wk->heapID);
	PaletteFadeWorkAllocSet( wk->palAnm, FADE_MAIN_BG, FADE_PAL_ONE_SIZE*AN_PUZZLE_BG_PALMAX,wk->heapID);
	PaletteFadeWorkAllocSet( wk->palAnm, FADE_SUB_BG, FADE_PAL_ONE_SIZE*AN_PUZZLE_BG_PALMAX,wk->heapID);
	PaletteFadeWorkAllocSet( wk->palAnm, FADE_MAIN_OBJ, FADE_PAL_ONE_SIZE*AN_PUZZLE_OBJ_PALMAX,wk->heapID);

	PaletteWorkSetEx_ArcHandle(wk->palAnm,handle,
		NARC_an_puzzle_gra_bg_nclr,wk->heapID,FADE_MAIN_BG,FADE_PAL_ONE_SIZE*AN_PUZZLE_BG_PALMAX,0,0);
	PaletteWorkSetEx_ArcHandle(wk->palAnm,handle,
		NARC_an_puzzle_gra_bg_nclr,wk->heapID,FADE_SUB_BG,FADE_PAL_ONE_SIZE*AN_PUZZLE_BG_PALMAX,0,0);
	PaletteWorkSetEx_ArcHandle(wk->palAnm,handle,
		NARC_an_puzzle_gra_oam_nclr,wk->heapID,FADE_MAIN_OBJ,FADE_PAL_ONE_SIZE*AN_PUZZLE_OBJ_PALMAX,0,0);


	//サブ上画面背景転送
	ArcUtil_HDL_BgCharSet( handle,
			NARC_an_puzzle_gra_bgd_back_ncgr, wk->bgl, UFRM_BACK, 0, 0, FALSE, wk->heapID);
	ArcUtil_HDL_ScrnSet( handle,
			NARC_an_puzzle_gra_bgu_back_nscr, wk->bgl, UFRM_BACK, 0, 0, FALSE, wk->heapID);
	ArcUtil_HDL_ScrnSet( handle,
			NARC_an_puzzle_gra_bgu_stone_nscr, wk->bgl, UFRM_STONE, 0, 0, FALSE, wk->heapID);

	//メイン下画面背景転送
	ArcUtil_HDL_BgCharSet( handle,
			NARC_an_puzzle_gra_bgd_back_ncgr, wk->bgl, DFRM_BACK, 0, 0, FALSE, wk->heapID);
	ArcUtil_HDL_ScrnSet( handle,
			NARC_an_puzzle_gra_bgd_back_nscr, wk->bgl, DFRM_BACK, 0, 0, FALSE, wk->heapID);

	//書換えパーツのスクリーンデータを取得
	wk->pSPartsBuf = ArcUtil_HDL_ScrnDataGet(handle,
		NARC_an_puzzle_gra_bgd_sub_nscr,FALSE,&wk->pSParts,wk->heapID);

	//ハンドル解放
	ArchiveDataHandleClose( handle );

	// 会話フォントパレット転送
	PaletteWorkSet_Arc(wk->palAnm,ARC_WINFRAME,NARC_winframe_talk_win00_nclr + wk->win_type,wk->heapID,
		FADE_MAIN_BG,FADE_PAL_ONE_SIZE,16*BMP_TALK_WIN_PAL);
	PaletteWorkSet_Arc(wk->palAnm,ARC_FONT,NARC_font_talk_ncrl,wk->heapID,
		FADE_MAIN_BG,FADE_PAL_ONE_SIZE,16*TALK_FONT_PAL);

	//会話ウインドウグラフィック転送
	TalkWinGraphicSet(wk->bgl,DFRM_MSG,
		BMP_TALK_WIN_CGX, BMP_TALK_WIN_PAL,wk->win_type, wk->heapID);

	//パレット初期転送
	PaletteTrans_AutoSet(wk->palAnm,TRUE);
	PaletteFadeTrans( wk->palAnm );

	//ブレンド設定
//	G2_SetBlendAlpha(ALPHA_MASK_1ST,ALPHA_MASK_2ND,ALPHA_SECOND,ALPHA_FIRST);
}

/**
 * @brief	2Dリソース解放
 */
static void sub_2DResRelease(AN_PUZZLE_WK* wk)
{
//	G2_SetBlendAlpha(GX_BLEND_PLANEMASK_NONE,GX_BLEND_PLANEMASK_NONE,31,0);

	sys_FreeMemoryEz(wk->pSPartsBuf);

	// パレットフェード開放
	PaletteFadeWorkAllocFree( wk->palAnm, FADE_MAIN_OBJ );
	PaletteFadeWorkAllocFree( wk->palAnm, FADE_SUB_BG );
	PaletteFadeWorkAllocFree( wk->palAnm, FADE_MAIN_BG );

	//パレットフェードシステム開放
	PaletteFadeFree( wk->palAnm );
}

/**
 *	@brief	メッセージデータ初期化
 */
static void sub_MsgDataInit(AN_PUZZLE_WK* wk)
{
	int i;

	//タッチフォントロード
	FontProc_LoadFont(FONT_TOUCH,wk->heapID);

	wk->msg.man = MSGMAN_Create(MSGMAN_TYPE_NORMAL,ARC_MSG,
			NARC_msg_an_puzzle_dat,wk->heapID);

	wk->msg.wset = WORDSET_CreateEx(WORDSET_AN_PUZZLE_BUFNUM,WORDSET_AN_PUZZLE_BUFLEN,wk->heapID);
	wk->msg.tmp = STRBUF_Create(AN_PUZZLE_MSG_BUFLEN,wk->heapID);

	wk->msg.back = MSGMAN_AllocString(wk->msg.man,msg_panel_back);
	for(i = 0;i < MIDX_POKE_MAX;i++){
		wk->msg.poke[i] = MSGMAN_AllocString(wk->msg.man,msg_exp_lv01+i);
	}
	for(i = 0;i < MIDX_EXP_MAX;i++){
		wk->msg.exp[i] = MSGMAN_AllocString(wk->msg.man,msg_exp_endchk+i);
	}
}

/**
 *	@brief	メッセージデータ解放
 */
static void sub_MsgDataRelease(AN_PUZZLE_WK* wk)
{
	int i;

	//文字列バッファ削除
	for(i = 0;i < MIDX_EXP_MAX;i++){
		STRBUF_Delete(wk->msg.exp[i]);
	}
	for(i = 0;i < MIDX_POKE_MAX;i++){
		STRBUF_Delete(wk->msg.poke[i]);
	}
	STRBUF_Delete(wk->msg.back);
	STRBUF_Delete(wk->msg.tmp);
	WORDSET_Delete(wk->msg.wset);

	MSGMAN_Delete(wk->msg.man);

	//タッチフォントアンロード
	FontProc_UnloadFont(FONT_TOUCH);
}

/**
 *	@brief	BMPウィンドウ初期化
 */
static void sub_BmpWinAdd(AN_PUZZLE_WK* wk)
{
	int i;
	static const BMPWIN_DAT BmpWinData[] = {
	 {	// 「もどるボタン」
		BMP_BACK_FRM, BMP_BACK_PX, BMP_BACK_PY,
		BMP_BACK_SX, BMP_BACK_SY, BMP_BACK_PAL, BMP_BACK_CGX
	 },
	 {	// 「メッセージウィンドウ」
		BMP_MSG_FRM, BMP_MSG_PX, BMP_MSG_PY,
		BMP_MSG_SX, BMP_MSG_SY, BMP_MSG_PAL, BMP_MSG_CGX
	 },
	 {	// 「パズルポケモン説明ウィンドウ」
		BMP_POKE_FRM, BMP_POKE_PX, BMP_POKE_PY,
		BMP_POKE_SX, BMP_POKE_SY, BMP_POKE_PAL, BMP_POKE_CGX
	 },
	};

	for( i=0; i<WIN_MAX; i++ ){
		GF_BGL_BmpWinAddEx( wk->bgl, &wk->win[i], &BmpWinData[i] );
		GF_BGL_BmpWinDataFill( &wk->win[i], 0 );
	}

	//YesNoボタンシステムワーク確保
	wk->ynbtn_wk = TOUCH_SW_AllocWork(wk->heapID);
}

/**
 *	@brief	BMPウィンドウ解放
 */
static void sub_BmpWinDel(AN_PUZZLE_WK* wk)
{
	int i;
	for( i=0; i<WIN_MAX; i++ ){
		GF_BGL_BmpWinOff( &wk->win[i] );
		GF_BGL_BmpWinDel( &wk->win[i] );
	}
	//YesNoボタンシステムワーク解放
	TOUCH_SW_FreeWork( wk->ynbtn_wk );
}


/**
 *	@brief	セルアクターシステム初期化
 */
static void sub_ActorSysInit(AN_PUZZLE_WK* wk)
{
	initVramTransferManagerHeap(32,wk->heapID);

	//セルアクターシステム初期化
	wk->pActSys = CATS_AllocMemory(wk->heapID);

	CATS_SystemInit( wk->pActSys,
		&an_puzzle_coi,&an_puzzle_ccmm,AN_PUZZLE_ACTPAL_NUM );

	//OAM初期化
	REND_OAM_UtilOamRamClear_Main(wk->heapID);
	REND_OAM_UtilOamRamClear_Sub(wk->heapID);

	//セルアクターリソース初期化
	wk->pActRes = CATS_ResourceCreate(wk->pActSys);
	CATS_ClactSetInit( wk->pActSys, wk->pActRes,AN_PUZZLE_ACTMAX);

	CATS_ResourceLoadBinaryEx( wk->pActSys, wk->pActRes,
		&an_puzzle_actres_list,CATS_CGXOFS_OFFSET,CATS_PLTTOFS_OFFSET);

}

/**
 *	@brief	セルアクターシステム解放
 */
static void sub_ActorSysRelease(AN_PUZZLE_WK* wk)
{
	//リソース解放
	CATS_ResourceDestructor(wk->pActSys,wk->pActRes);
	wk->pActRes = NULL;

	//システムワーク解放
	CATS_FreeMemory(wk->pActSys);
	wk->pActSys = NULL;
	DellVramTransferManager();

	//OAM初期化
	REND_OAM_UtilOamRamClear_Main(wk->heapID);
}

/*
 *	@brief	セルアクター初期化
 */
static void sub_ActorInit(AN_PUZZLE_WK* wk)
{
	sub_ActorSysInit(wk);
	sub_ActorObjMake(wk);

	GF_Disp_GX_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON );
}

/**
 *	@brief	セルアクター解放
 */
static void sub_ActorRelease(AN_PUZZLE_WK* wk)
{
	GF_Disp_GX_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_OFF );

	sub_ActorObjDelete(wk);
	sub_ActorSysRelease(wk);
}

/**
 *	@brief	セルアクターオブジェクト作成
 */
static void sub_ActorObjMake(AN_PUZZLE_WK* wk)
{
	int i;

	static const TCATS_OBJECT_ADD_PARAM	ActAddParam[] =
	{
		{	// 戻るボタン
			ACTRES_NORMAL,	//ID
			ACT_RET_PX, ACT_RET_PY, 0,	//x,y,z
			ANM_RET, ACT_SPRI_RET, 0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// パネルの影
			ACTRES_NORMAL,	//ID
			ACT_RET_PX, ACT_RET_PY, 0,	//x,y,z
			ANM_SHADOW, ACT_SPRI_SHADOW, 0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// パネル
			ACTRES_PUZZLE01,	//ID
			ACT_RET_PX, ACT_RET_PY, 0,	//x,y,z
			ANM_RET, ACT_SPRI_PANEL, 0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
	};

	for(i = 0;i <= ACT_SHADOW;i++){
		wk->pAct[i] = CATS_ObjectAdd( wk->pActSys, wk->pActRes, &ActAddParam[i] );
		CLACT_SetAnmFrame(wk->pAct[i], FX32_ONE);
	}
	CLACT_SetDrawFlag(wk->pAct[ACT_RET], TRUE );
	CLACT_SetDrawFlag(wk->pAct[ACT_SHADOW], FALSE );
	CLACT_SetAnmFlag(wk->pAct[ACT_RET], TRUE);
	CLACT_ObjModeSet( wk->pAct[ACT_SHADOW],GX_OAM_MODE_NORMAL);
	CLACT_BGPriorityChg( wk->pAct[ACT_SHADOW], DFRM_EFF_PRI );
	CLACT_ObjModeSet( wk->pAct[ACT_SHADOW],GX_OAM_MODE_XLU);


	//パネルセルのキャラクタリソース読み替え
	CATS_ChangeResourceCharArc(
		wk->pActSys, wk->pActRes, ARC_AN_PUZZLE_GRA,
		NARC_an_puzzle_gra_oam_p01_ncgr+wk->level,FALSE, ACTRES_PUZZLE01);

	//パズルパネル作成
	for(i = 0;i < PANEL_MAX;i++){
		u8 idx;

		idx = i+ACT_PANEL01;
		wk->pAct[idx] = CATS_ObjectAdd( wk->pActSys,wk->pActRes,&ActAddParam[2] );
		CLACT_SetDrawFlag(wk->pAct[idx], TRUE );
		CLACT_AnmChg(wk->pAct[idx], i );
		CLACT_ObjModeSet( wk->pAct[idx],GX_OAM_MODE_NORMAL);

		//回転させるとドット欠けするので倍角設定にする
		CLACT_SetAffineParam(  wk->pAct[idx], CLACT_AFFINE_DOUBLE);

		//パネルワークにアクターポインタ設定
		wk->panel[i].act = wk->pAct[idx];

		//初期表示位置セット
		sub_PanelGridPosSet(wk,i,wk->panel[i].x,wk->panel[i].y,wk->panel[i].rot);
	}
}

/**
 *	@brief	セルアクターオブジェクト削除
 */
static void sub_ActorObjDelete(AN_PUZZLE_WK* wk)
{
	int i;

	for(i = 0;i < AN_PUZZLE_ACTMAX;i++){
		CLACT_Delete(wk->pAct[i]);
	}
}

/**
 *	@brief	パズル完成チェック
 */
static BOOL sub_IsPerfect(AN_PUZZLE_WK* wk)
{
	int i;

	for(i = 0;i < PANEL_MAX;i++){
		if(	wk->panel[i].rot != ROT_U ||
			wk->panel[i].x != ((i%PANEL_COMP_FLDX)+1) ||
			wk->panel[i].y != ((i/PANEL_COMP_FLDX)+1)){
			return FALSE;
		}
	}
	return TRUE;
}

/*
 *	@brief	やめるボタン書き込み
 */
static void sub_BackButtonWrite(AN_PUZZLE_WK* wk,u8 pat)
{
	u32 siz;
	static const GF_PRINTCOLOR col[] = {
	 GF_PRINTCOLOR_MAKE(1,2,0),
	 GF_PRINTCOLOR_MAKE(3,4,0),
	 GF_PRINTCOLOR_MAKE(5,6,0),
	};

	GF_BGL_BmpWinDataFill( &wk->win[WIN_RET],0);

	CLACT_AnmChg(wk->pAct[ACT_RET],pat);
	if(pat == ANM_RETA){
		CLACT_AnmReStart(wk->pAct[ACT_RET]);
		CLACT_SetAnmFlag(wk->pAct[ACT_RET],TRUE);
	}else{
		CLACT_SetAnmFlag(wk->pAct[ACT_RET],FALSE);
	}
	siz = FontProc_GetPrintStrWidth( FONT_TOUCH, wk->msg.back, 0 );
	GF_STR_PrintColor( &wk->win[WIN_RET], FONT_TOUCH, wk->msg.back,
		((BMP_BACK_SX*8)-siz)/2,4, MSG_NO_PUT, col[pat], NULL );

	GF_BGL_BmpWinOnVReq(&wk->win[WIN_RET]);
}

/**
 *	@brief	ウィンドウメッセージ書き込み
 */
static void sub_WinMsgWrite(AN_PUZZLE_WK* wk,u8 midx,BOOL winf_on,u8 spd)
{
	if(winf_on){
		BmpTalkWinWrite( &wk->win[WIN_MSG],
			WINDOW_TRANS_OFF, BMP_TALK_WIN_CGX, BMP_TALK_WIN_PAL);
	}

	GF_BGL_BmpWinDataFill( & wk->win[WIN_MSG], FBMP_COL_WHITE );
	if(spd == 0){
		GF_STR_PrintColor( &wk->win[WIN_MSG], FONT_TALK, wk->msg.exp[midx],
			0,0, MSG_NO_PUT, FCOL_W_BLACK, NULL );
	}else{
		wk->msg_idx = GF_STR_PrintColor( &wk->win[WIN_MSG], FONT_TALK, wk->msg.exp[midx],
			0,0, spd, FCOL_W_BLACK, NULL );
	}

	GF_BGL_BmpWinOnVReq(&wk->win[WIN_MSG]);
}

/**
 *	@brief	パズル ポケモンヒントメッセージ
 */
static void sub_WinPokeWrite(AN_PUZZLE_WK* wk)
{
	GF_BGL_BmpWinDataFill( & wk->win[WIN_POKE], 0 );
	GF_STR_PrintColor( &wk->win[WIN_POKE], FONT_TOUCH, wk->msg.poke[wk->level],
		0,0, MSG_NO_PUT, FCOL_N_POKE, NULL );

	GF_BGL_BmpWinOnVReq(&wk->win[WIN_POKE]);
}

/**
 *	@brief	移動先ガイド書き込みコア
 */
static void sub_PanelGuideDrawCore(AN_PUZZLE_WK* wk,u8 gx,u8 gy,u8 pat)
{
	if(pat == G_NULL){
		GF_BGL_ScrFill(wk->bgl, DFRM_EFF, 0x00,
			gx*GRID_BG_W+GRID_BG_PX,
			gy*GRID_BG_H+GRID_BG_PY, GRID_BG_W,GRID_BG_H, 0);
	}else{
		GF_BGL_ScrWriteExpand(wk->bgl,DFRM_EFF,
			gx*GRID_BG_W+GRID_BG_PX,gy*GRID_BG_H+GRID_BG_PY,
			GRID_BG_W,GRID_BG_H,wk->pSParts->rawData,
			GRID_BG_W*(pat-1),0,wk->pSParts->screenWidth/8,wk->pSParts->screenHeight/8);
	}
}

/**
 *	@brief	移動先ガイド表示
 */
static void sub_PanelGuideDraw(AN_PUZZLE_WK* wk,u8 gx,u8 gy)
{
	//古い座標をクリア
	if(wk->target->x == wk->gpos_x && wk->target->y == wk->gpos_y){
		sub_PanelGuideDrawCore(wk,wk->gpos_x,wk->gpos_y,G_TARGET);
	}else{
		sub_PanelGuideDrawCore(wk,wk->gpos_x,wk->gpos_y,G_NULL);
	}
	GF_BGL_LoadScreenV_Req( wk->bgl, DFRM_EFF );

	if(gx == 0 && gy == 0){
		return;	//新しい座標の書き込みは無し
	}
	if(wk->target->x == gx && wk->target->y == gy){
		sub_PanelGuideDrawCore(wk,gx,gy,G_CONF);
	}else{
		sub_PanelGuideDrawCore(wk,gx,gy,G_MOVE);
	}
}

/**
 *	@brief	キーorタッチ切替ステータスチェンジ
 */
static int sub_InputChangeSelect(AN_PUZZLE_WK* wk)
{
	if(wk->key_mode == APP_KTST_KEY){
		wk->key_mode = APP_KTST_TOUCH;
	}else{
		wk->key_mode = APP_KTST_KEY;
		return TRUE;
	}
	return FALSE;
}

/**
 *	@brief	YesNoボタン初期化
 */
static void sub_YesNoBtnSet(AN_PUZZLE_WK* wk)
{
	TOUCH_SW_PARAM param;
	MI_CpuClear8(&param,sizeof(TOUCH_SW_PARAM));

	param.p_bgl  = wk->bgl;
	param.bg_frame = BMP_YESNO_FRM;
	param.char_offs = BMP_YESNO_CGX;
	param.pltt_offs = BMP_YESNO_PAL;
	param.x		= BMP_YESNO_PX;
	param.y		= BMP_YESNO_PY;
	param.kt_st = wk->key_mode;
	param.key_pos = 1;
	param.type = TOUCH_SW_TYPE_S;
	TOUCH_SW_InitEx( wk->ynbtn_wk, &param ,wk->palAnm);
}

/**
 *	@brief	YesNoボタン待ち
 */
static int sub_YesNoBtnWait(AN_PUZZLE_WK* wk)
{
	int ret;
	int	mode;

	ret = TOUCH_SW_Main( wk->ynbtn_wk );

	switch(ret){
	case TOUCH_SW_RET_YES:
		ret = SEQ_FADE_OUT;
		break;
	case TOUCH_SW_RET_NO:
		ret = SEQ_PUZZLE_MAIN;
		break;
	default:
		return SEQ_END_CHECK;
	}
	wk->key_mode = TOUCH_SW_GetKTStatus(wk->ynbtn_wk);
	TOUCH_SW_Reset( wk->ynbtn_wk);

	BmpTalkWinClear( &wk->win[WIN_MSG], WINDOW_TRANS_OFF);
	GF_BGL_BmpWinDataFill( & wk->win[WIN_MSG], 0 );
	GF_BGL_BmpWinOnVReq( &wk->win[WIN_MSG]);
	GF_BGL_LoadScreenV_Req( wk->bgl, DFRM_MSG );
	return ret;
}

/**
 *	@brief	パネルアクター位置セット
 */
static void sub_PanelActPosSet(PANEL_WK* pp, s16 x, s16 y)
{
	static const s8 ofs[4][2] = { {0,0},{-1,0},{-1,-1},{0,-1} };

	//アフィン機能で回転させると、ドット位置がずれるので表示オフセットをずらしてごまかす
	CATS_ObjectPosSet(pp->act,
		x+ofs[pp->rot][0],y+ofs[pp->rot][1]);
}

/**
 *	@brief	パネル位置グリッドセット
 */
static void sub_PanelGridPosSet(AN_PUZZLE_WK* wk,u8 idx,u8 x,u8 y,u8 rot)
{
	PANEL_WK* pp;

	//パネルワークに新ポジションセット
	pp = &wk->panel[idx];
	pp->x = x;
	pp->y = y;
	pp->rot = rot;

	sub_PanelActPosSet(pp, GridPosX(x),GridPosY(y));

	CLACT_SetRotation(pp->act, rot*ROT90 );
}

/***/
/*
 *	@brief	ターゲットパネルステータスセット
 */
static void sub_TargetPanelStateSet(AN_PUZZLE_WK* wk,u8 idx,BOOL set)
{
	//ターゲット設定
	if(set){
		wk->tidx = idx;
		wk->target = &wk->panel[wk->tidx];

		CLACT_DrawPriorityChg( wk->target->act, ACT_SPRI_TARGET);
		CATS_ObjectPosMove(wk->target->act, ACT_PANEL_OFS, ACT_PANEL_OFS);

	//	CLACT_SetDrawFlag(wk->target->act, FALSE);
		//影セット
		CLACT_SetDrawFlag(wk->pAct[ACT_SHADOW], TRUE);
		CATS_ObjectPosSet(wk->pAct[ACT_SHADOW],GridPosX(wk->target->x),GridPosY(wk->target->y));

		//戻る表示off
		sub_BackButtonWrite(wk,ANM_RETP);
	}else{
//		CLACT_SetDrawFlag(wk->target->act, TRUE);
		CLACT_DrawPriorityChg( wk->target->act, ACT_SPRI_PANEL);
		sub_PanelGridPosSet(wk,wk->tidx,wk->target->x,wk->target->y,wk->target->rot);

		CLACT_SetDrawFlag(wk->pAct[ACT_SHADOW], FALSE);

		//戻る表示on
		sub_BackButtonWrite(wk,ANM_RET);

		wk->gpos_x = 0;
		wk->gpos_y = 0;
		wk->target = NULL;
		wk->tidx = 0;
	}
}

/**
 *	@brief	やめるボタンアニメタスクセット
 */
static void sub_BackBtnAnimeSet(AN_PUZZLE_WK* wk)
{
	BACKBTN_TASK_WK* btk;

	btk = sys_AllocMemoryLo(wk->heapID,sizeof(BACKBTN_TASK_WK));
	MI_CpuClear8(btk,sizeof(BACKBTN_TASK_WK));

	btk->paw = wk;

	// タスク登録
	TCB_Add( tcb_BackBtnAnime, btk, 0);
	sub_BackButtonWrite(wk,ANM_RETA);
	wk->btnanm_f  = TRUE;
}

/*
 *	@brief	やめるボタンアニメタスク
 */
static void tcb_BackBtnAnime( TCB_PTR tcb, void* p_work )
{
	BACKBTN_TASK_WK* btk = (BACKBTN_TASK_WK*)p_work;
	AN_PUZZLE_WK* wk = btk->paw;

	if(CLACT_AnmActiveCheck( wk->pAct[ACT_RET])){
		return;
	}
	sub_BackButtonWrite(btk->paw,ANM_RET);

	btk->paw->btnanm_f = FALSE;
	MI_CpuClear8(btk,sizeof(BACKBTN_TASK_WK));
	sys_FreeMemoryEz(btk);
	// メモリ解放
	TCB_Delete( tcb );
}